//
//  GFNearHomeDetialViewController.m
//  GoodFood
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFNearHomeDetialViewController.h"

@interface GFNearHomeDetialViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *detialCollectionView;
@property (nonatomic, assign) NSInteger contentOffset;
@property (nonatomic, copy) NSString *str;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) NSMutableArray *foodmarkArr;
@end

@implementation GFNearHomeDetialViewController

-(void)dealloc {
    
    [self.detialCollectionView removeObserver:self forKeyPath:@"contentOffset"];
}

-(void)viewWillAppear:(BOOL)animated {
    
    self.detialCollectionView.contentOffset = CGPointMake(self.num * ScreenWidth, 0);
    
}

-(void)handleDataForMoreFoodmark {
    self.app = [[UIApplication sharedApplication] delegate];
    NSString *str = [NSString stringWithFormat:@"http://api.meishixing.com/tweet/tweet/detail/session_id=%@&tweet_id=%@",self.app.session_id, self.tweet_id];
    
    [AFNetHandle GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        self.foodmarkArr = [NSMutableArray array];
        GFNearHomeModel *model = [GFNearHomeModel modWithDic:result[@"result"]];
        [self.foodmarkArr addObject:model];
        [self.detialCollectionView reloadData];
    } fail:^(NSError *error) {
    }];

    
    
}

-(void)handleDataForFoodmark {
    self.app = [[UIApplication sharedApplication] delegate];
    NSString *str = [NSString stringWithFormat:@"http://api.meishixing.com/picture/picture/detailall/session_id=%@&pic_id=%@",self.app.session_id, self.picture_id];
    
    [AFNetHandle GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        self.arr = [NSMutableArray array];
        GFNearHomeModel *model = [GFNearHomeModel modWithDic:result[@"result"]];
        [self.arr addObject:model];
        [self.detialCollectionView reloadData];
    } fail:^(NSError *error) {
    }];
    
    
}

-(void)handleData:(NSInteger)page {
    self.app = [[UIApplication sharedApplication] delegate];
    if (self.tag == 1) {
        self.str = self.url;
    } else {
        self.str = [NSString stringWithFormat:@"http://api.meishixing.com/picture/picturelist/last/page=%ld&city_id=%@&session_id=%@", self.page, self.city_id, self.app.session_id];
    }
    [AFNetHandle GETWithURL:self.str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSArray *tempArr = [result objectForKey:@"result"];
        for (NSDictionary *dic in tempArr) {
            GFNearHomeModel *model = [GFNearHomeModel modWithDic:dic];
            [self.detialArr addObject:model];
        }
        [self.detialCollectionView reloadData];
    } fail:^(NSError *error) {

    }];
}


-(void)creatCollectionView {
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(ScreenWidth, ScreenHeight - 64);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.detialCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) collectionViewLayout:flow];
    self.detialCollectionView.backgroundColor = [UIColor whiteColor];
//    self.detialCollectionView.showsHorizontalScrollIndicator = NO;
    self.detialCollectionView.delegate = self;
    self.detialCollectionView.dataSource = self;
    self.detialCollectionView.pagingEnabled= YES;
    [self.view addSubview:self.detialCollectionView];
    
    //KVO监听当前偏移量
    [self.detialCollectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    [self.detialCollectionView registerClass:[GFDetialCollectionViewCell class] forCellWithReuseIdentifier:@"detial"];
    
    
    
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    //获取当前的偏移量
    CGPoint point = [[change objectForKey:@"new"] CGPointValue];
    self.contentOffset = point.x;
    self.num = self.contentOffset / ScreenWidth;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.i == 200) {
        return self.arr.count;
    } else if (self.i == 300) {
        return self.foodmarkArr.count;
    }
    else {
        return self.detialArr.count;
    }
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    GFDetialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"detial" forIndexPath:indexPath];
    GFNearHomeModel *model;
    if (self.i == 200) {
        model = [self.arr objectAtIndex:indexPath.item];
    } else if (self.i == 300) {
        model = [self.foodmarkArr objectAtIndex:indexPath.item];
    } else {
        model = [self.detialArr objectAtIndex:indexPath.item];
    }
    
    void(^block)(void) = ^(void) {

        
        GFBigFoodImageViewController *bigFoodImageVC = [[GFBigFoodImageViewController alloc] init];
        bigFoodImageVC.url = model.picture_url;
        
        
        bigFoodImageVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:bigFoodImageVC animated:YES completion:^{
            
        }];
    };

    cell.tweetID = model.tweet_id;
    cell.block = block;
    return cell;
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    if (self.i != 200 && self.i != 300) {
        //初始化NSIndexPath 用来获取当前cell的位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.contentOffset / ScreenWidth inSection:0];
        GFDetialCollectionViewCell *cell = (GFDetialCollectionViewCell *)[self.detialCollectionView cellForItemAtIndexPath:indexPath];
        GFNearHomeModel *model = [self.detialArr objectAtIndex:indexPath.item];
        cell.tweetID = model.tweet_id;
        if (indexPath.item == self.detialArr.count - 1) {
            self.page++;
            [self handleData:self.page];
        }
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creatCollectionView];
//    [self.view bringSubviewToFront:self.waitView];
    [self.waitView removeFromSuperview];
    [self handleDataForFoodmark];
    [self handleDataForMoreFoodmark];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
